using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class NewsTypeDAL
    {
        public static int Save(NewsTypeInfo info)
        {
            string sql = string.Format("insert into [newstype]([AdminID],[Name],[Sort]) values(@AdminID,@Name,@Sort);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Sort", info.Sort)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(NewsTypeInfo info)
        {
            string sql = string.Format("update [newstype] set [AdminID]=@AdminID,[Name]=@Name,[Sort]=@Sort where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static NewsTypeInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[newstype]");
            return SimpleData(dr);
        }
        public static List<NewsTypeInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[newstype]");
            return ListSimpleData(dt);
        }
        public static List<NewsTypeInfo> FindPage(string keyword, int adminid, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            filter.AppendFormat(" and [newstype].AdminID={0}", adminid);
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [newstype].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [newstype].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[newstype]", out count);
            return ListSimpleData(dt);
        }
        public static List<NewsTypeInfo> FindAll(int adminid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [newstype] where AdminID={0}", adminid));
            return ListSimpleData(dt);
        }
        private static NewsTypeInfo SimpleData(DataRow dr)
        {
            NewsTypeInfo info = new NewsTypeInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
            }
            return info;
        }
        private static NewsTypeInfo AllData(DataRow dr)
        {
            NewsTypeInfo info = new NewsTypeInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
            }
            return info;
        }
        private static List<NewsTypeInfo> ListSimpleData(DataTable dt)
        {
            List<NewsTypeInfo> list = new List<NewsTypeInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<NewsTypeInfo> ListAllData(DataTable dt)
        {
            List<NewsTypeInfo> list = new List<NewsTypeInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

